<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Site/classes/PhpFox_ServiceBan.class.php, $Author:  $ */


/** Implements functions for working with ban system
 *
 */
class PhpFox_Mod_Site_ServiceBan extends BaseService
{
    function checkIPBan()
    {
    	$oCache = &App::newObject("Cache");
		$oCache->setFile("banIp.php");
		if ( !$oCache->isCached() )
		{
			$aContent = array();
			$aRows = $this->oDatabase->getRows("SELECT `ip`, `block_until` FROM ". App::getT('ban_ip') ."");
	    	foreach($aRows as $aBan)
	    	{
	    		$aContent[$aBan['ip']] = $aBan['block_until'];
	    	}

	    	$oCache->save($aContent);
	    }

		$aIps = $oCache->doRequire();
		if (isset($aIps[$_SERVER['REMOTE_ADDR']]))
		{
			if ($aIps[$_SERVER['REMOTE_ADDR']] == '0000-00-00 00:00:00' || $aIps[$_SERVER['REMOTE_ADDR']] > Database::date())
			{
				$this->oDatabase->_delete(App::getT('online_session'), "online_session_user = 'online_session_user'");
				$oSrvSecurity =& App::getModuleService('Account', 'Security');
				$oSrvSecurity->logout();				
				exit('IP banned!');
			}
		}

   		return true;
    }

    function clearBanList()
    {
        $oIPItem = &$this->_oModule->getItem('IP');
        /* @var $oIPItem PhpFox_Mod_Site_ItemIP */

        $sSql = 'DELETE FROM '.App::getT('ban_ip').' '.
               ' WHERE block_until < "'.Database::date().'" '.
               '   AND block_until <> "0000-00-00 00:00:00"';
        $oIPItem->oDb->query($sSql);
        return true;
    }

    function getBlockedLogins()
    {
    	$aItems = array();
    	$aContent = $this->oDatabase->getRows("SELECT value FROM ". App::getT('ban') ." WHERE type = 'signup'");
    	foreach($aContent as $aBan)
    	{
    		$aItems[] = strtolower($aBan['value']);
    	}
    	return $aItems;
    }

	function checkDirs($sUser)
	{
		$mDir = opendir(App::getParam('path'));
		while( $sFile = readdir($mDir) )
		{
			if ( $sFile != '.' && $sFile != '..' && (is_dir($sFile)) )
			{
				if ( $sUser == $sFile )
				{
					return false;
				}
			}
		}
		return true;
	}

    function checkEmail($sCheckEmail)
    {
    	$aEmails = array();
    	$aContent = $this->oDatabase->getRows("SELECT value FROM ". App::getT('ban') ." WHERE type = 'email'");
    	foreach($aContent as $aBan)
    	{
            if ($sCheckEmail == $aBan['value'])
            {
            	return false;
            }
        	elseif (strpos($sCheckEmail, '@' . $aBan['value']))
            {
                return false;
            }
        }
        return true;
    }

    function runParseString($sTxt, $aWords)
    {
    	foreach ($aWords as $aWord)
        {
        	if ( isset($aWord[0]) )
        	{
        		if (empty($aWord[1]) )
        		{
        			$iLength = strlen($aWord[0]);
        			$sSub = '';
        			for($i = 0; $i <= $iLength; $i++)
        			{
        				$sSub .= '#';
        			}
        			$aWord[1] = $sSub;
        		}
        		else
        		{
        			$aWord[1] = $aWord[1];
        		}

    			$sTxt = " ". $sTxt ." ";
        		$sTxt = eregi_replace(" ". $aWord[0] ." ", " ". $aWord[1] ." ", $sTxt);
        		$sTxt = eregi_replace(">". $aWord[0] ."<", ">". $aWord[1] ."<", $sTxt);
        		$sTxt = eregi_replace(">". $aWord[0] ." ", ">". $aWord[1] ." ", $sTxt);
        		$sTxt = eregi_replace(" ". $aWord[0] ."<", " ". $aWord[1] ."<", $sTxt);
        		$sTxt = eregi_replace("&nbsp;". $aWord[0] ."&nbsp;", "&nbsp;". $aWord[1] ."&nbsp;", $sTxt);
        		$sTxt = eregi_replace("&nbsp;". $aWord[0] ."<", "&nbsp;". $aWord[1] ."<", $sTxt);
        		$sTxt = eregi_replace(">". $aWord[0] ."&nbsp;", ">". $aWord[1] ."&nbsp;", $sTxt);
        		$sTxt = eregi_replace(" ". $aWord[0] ."<", " ". $aWord[1] ."<", $sTxt);
        		$sTxt = eregi_replace(">". $aWord[0] ." ", ">". $aWord[1] ." ", $sTxt);
        		
        		if (preg_match("/^(http|ftp|https):\/\/(.*)$/i", $aWord[0]))
        		{
        			$sTxt = preg_replace("/href=\"". str_replace(array('/', '?'), array("\\/", "\\?"), $aWord[0]) ."\"/i", 'href="' . App::getParam('url') . '"', $sTxt);
        			$sTxt = preg_replace("/href='". str_replace(array('/', '?'), array("\\/", "\\?"), $aWord[0]) ."'/i", 'href="' . App::getParam('url') . '"', $sTxt);
        			$sTxt = preg_replace("/". str_replace(array('/', '?'), array("\\/", "\\?"), $aWord[0]) ."/i", $aWord[1], $sTxt);
        		}        		
			}
    	}

    	$sTxt = ltrim($sTxt);
    	$sTxt = rtrim($sTxt);

    	return $sTxt;
    }

    function parseStringForBadwords($sTxt)
    {
    	static $sWords = array();

    	if ( $sWords )
    	{

    		$sTxt = $this->runParseString($sTxt, $sWords);



    		return $sTxt;
    	}

		$oCache = &App::newObject("Cache");
		$oCache->setFile("ban_word.php");
		if ( !$oCache->isCached() )
		{
			$sContent = "";
			$aContent = $this->oDatabase->getRows("SELECT `value`, `replace` FROM ". App::getT('ban') ." WHERE `type` = 'word'");
	    	foreach($aContent as $aBan)
	    	{
	    		$sContent .= "array('". $aBan['value'] ."', '". $aBan['replace'] ."'),";
	    	}

	    	$oCache->save($sContent);
	    }

		$sWords = $oCache->doRequire();
		$sTxt = $this->runParseString($sTxt,$sWords);

        return $sTxt;
    }
}
?>